#include "ArgumentParser.h"
#include "Helper.h"

#include "Radar.h"
#include "CloneBot.h"

#include <string>
#include <iostream>
#include <stdio.h>

using namespace std;

class CloneHunt
{
public:
	bool m_bByName;
	bool m_bBySize;
	bool m_bByChecksum;
	bool m_bByNone;
	bool m_bMerge;

	string m_sExtension;
	string m_sIgnore;

	CloneHunt()
	{
		m_bByName = true;
		m_bBySize = false;
		m_bByChecksum = false;
		m_bByNone = false;
		m_bMerge = false;
	}
};

CArgumentParser* gpxArgumentParser;
string gsTarget = "./";
CloneHunt gxCloneHunt;
Radar gxRadar;
CloneBot gxCloneBot;


int parseCommandLineCloneHunt(int argc, char**argv)
{
	gpxArgumentParser=new CArgumentParser("cnsg:t:fmh");

	gpxArgumentParser->m_Helper->fillProgramName("CloneHunt");
	gpxArgumentParser->m_Helper->fillAuthor("SonOfDelphi");
	gpxArgumentParser->m_Helper->fillSummary("Duplicate file finder");
	gpxArgumentParser->m_Helper->fillUsageLine("[OPTIONS] <Directory>");
	gpxArgumentParser->fillHelpForOption('c',"Check using checksum (Mode not supported)");
	gpxArgumentParser->fillHelpForOption('n',"Check using name (Default mode, specify only for multimode)");
	gpxArgumentParser->fillHelpForOption('s',"Check using size");
	gpxArgumentParser->fillHelpForOption('g',"Source List ignore filename");
	gpxArgumentParser->fillHelpForOption('t',"Source List file-types(file extensions) to check ");
	gpxArgumentParser->fillHelpForOption('f',"No duplicate checks, just get file-list (Mode not supported)");
	gpxArgumentParser->fillHelpForOption('m',"Merge instead of filtering for multi-mode search");
	gpxArgumentParser->fillHelpForOption('h',"Displays this help message");

	gpxArgumentParser->m_Helper->fillYear("2009");
	gpxArgumentParser->m_Helper->fillOwner("The Oraclique");
	gpxArgumentParser->m_Helper->fillTailMessage("0.10 - Build:" __DATE__"/"__TIME__  "\n1.Checksum not supported.\n2.Only single argument supported for options: g & t.\n\nThis program comes with absolutely no warranty.");

	int nParseEndPos = gpxArgumentParser->parse(argc,argv);
	if(!gpxArgumentParser->wasParseSuccessful())
	{
		gpxArgumentParser->m_Helper->displayHelp();
		delete gpxArgumentParser;
		cerr<<"Invalid option supplied"<<endl;
		return -1;
	}

	gxCloneHunt.m_bByChecksum	= gpxArgumentParser->wasOptionSupplied('c');
	gxCloneHunt.m_bBySize		= gpxArgumentParser->wasOptionSupplied('s');
	gxCloneHunt.m_bByNone		= gpxArgumentParser->wasOptionSupplied('f');
	gxCloneHunt.m_bMerge		= gpxArgumentParser->wasOptionSupplied('m');
	gxCloneHunt.m_bByName		= (gxCloneHunt.m_bByChecksum | gxCloneHunt.m_bBySize | gxCloneHunt.m_bByNone)
		? gpxArgumentParser->wasOptionSupplied('n'):true;

	gxCloneHunt.m_sExtension = gpxArgumentParser->wasOptionSupplied('t')? gpxArgumentParser->getField('t'):"";
	gxCloneHunt.m_sIgnore	 = gpxArgumentParser->wasOptionSupplied('g')? gpxArgumentParser->getField('g'):"";

 	return nParseEndPos+1;
}

int main(int argc, char**argv)
{
	int nProgArgIndex = parseCommandLineCloneHunt(argc, argv);
	if(nProgArgIndex<0)
		return 1;

	if(gpxArgumentParser->wasOptionSupplied('h'))
	{
		gpxArgumentParser->m_Helper->displayHelp();
		delete gpxArgumentParser;
		return 0;
	}

	if(nProgArgIndex<argc)
	{
			gsTarget=argv[nProgArgIndex];
	}
	else
	{
			cerr<<"Attempting to proceed with default directory: "<<gsTarget<<endl;
	}
	
	cerr<<"Scanning "<< gsTarget << " ..." ;
	
	gxRadar.m_sIgnore = gxCloneHunt.m_sIgnore;
	gxRadar.m_sExtension = gxCloneHunt.m_sExtension;

	gxRadar.scan(gsTarget);
	cerr<<"found "<< gxRadar.getFileList()->m_apxItem.size() << " files." << endl;
	
	//gxRadar.dump();

	gxCloneBot.setSource(gxRadar.getFileList());

	if(gxCloneHunt.m_bByName)
	{
		gxCloneBot.findClones(CloneBot::eByName);
	}

	if(gxCloneHunt.m_bBySize)
	{
		if(!gxCloneHunt.m_bMerge) 		
			gxCloneBot.filterCurrentClones();
		gxCloneBot.findClones(CloneBot::eBySize);
	}
	
	gxCloneBot.dump();

	cerr<<"Press ENTER to continue..";
	getchar();


    return 0;
}


